
package infopanel;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.scene.Cursor;
import javafx.stage.Stage;
import queues.NotInitializedException;
import queues.QueuesClient;
import queues.UnknownSubjectException;
import server.Counter;
import server.IInfoPanelService;
import server.IInfoPanelServiceRegisterUnauthorizedAccessExceptionFaultFaultMessage;
import server.QueueInfo;
import server.QueuesService;
import server.Subject;

/**
 *
 * @author Renato Rosa <2101076@my.ipleiria.pt>
 */
public class InfoPanel extends QueuesClient<IInfoPanelService> implements SoundProducer {

    private InfoPanelWindowController controller;

    public InfoPanel() {
        super("infopanel.properties");
        notificationsService = new InfoPanelNotifService();
    }

//    @Override
//    public void init() throws Exception {
//        super.init();
//        if (properties != null) {
//            useSpeaker = Boolean.parseBoolean(properties.getProperty("useSpeaker", "false"));
////            if (useSpeaker) {
////                String speakerPath = properties.getProperty("speakSynthetizerPath");
////                String speakerArgs = properties.getProperty("speakSynthetizerArgs");
////                speaker = new Speaker();
////                speaker.init(speakerPath, speakerArgs);
////            }
//        }
//    }

    @Override
    public void start(Stage mainStage) throws Exception {
        super.start(mainStage);
        //Label label = LabelBuilder.create().text("Aguardando inicialização...").font(new Font("Arial", 20.0)).build();
        //BorderPane root = BorderPaneBuilder.create().center(label).prefHeight(900.0).prefWidth(1440.0).build();
        //stage.setScene(new Scene(root));
//        if (useSpeaker) {
//            speaker.start();
//        }
    }

    @Override
    public void stop() throws Exception {
        Speaker.getSpeaker().stop();
        super.stop();
    }

    @Override
    public void updateAll(List<Counter> counters, List<QueueInfo> queues) throws NotInitializedException, UnknownSubjectException {
        if (!initialized) {
            throw new NotInitializedException();
        }

        if (queues != null) {
            controller.setQueueInfos(queues);
        }
    }

    @Override
    public void initialize(List<Subject> subjects, List<Counter> counters, List<QueueInfo> tickets) throws NotInitializedException {
        super.initialize(subjects, counters, tickets);
        String fxml;
        switch (subjects.size()) {
            case 3:
                fxml = "3Panel1440x900.fxml";
                break;
            case 4:
                fxml = "4Panel1440x900.fxml";
                break;
            case 5:
                fxml = "5Panel1024x768.fxml";
                break;
            default:
                throw new NotInitializedException("Unable to find a layout for " + subjects.size() + " subjects");
        }

        try {
            controller = (InfoPanelWindowController) setSceneContent(fxml);
            controller.setSubjects(subjects);
            controller.setQueueInfos(tickets);
            initialized = true;
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    stage.getScene().setCursor(Cursor.NONE);
                }
            });
        } catch (Exception ex) {
            NotInitializedException e = new NotInitializedException();
            e.initCause(ex);
            throw e;
        }
    }

    @Override
    public void updateQueue(QueueInfo info) throws NotInitializedException, UnknownSubjectException {
        if (!initialized) {
            throw new NotInitializedException();
        }
        
        controller.updateQueue(info);
        
        try {
            Speaker.getSpeaker().advertise(info);
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            //do nothing
        } catch (IOException ex) {
            Logger.getLogger(InfoPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void registerImpl() throws IInfoPanelServiceRegisterUnauthorizedAccessExceptionFaultFaultMessage {
        proxy = new QueuesService(serverWsdlLocation).getInfoPanel();
        proxy.register(clientId);
    }

    @Override
    protected void unregisterImpl() throws Exception {
        proxy.unregister(clientId);
    }

    @Override
    public void setSoundOptions(boolean useAlert, boolean useVoice) {
        Speaker.getSpeaker().setUseAlert(useAlert);
        Speaker.getSpeaker().setUseVoice(useVoice);
    }
}